//
//  ViewController.m
//  CoreImageFun
//
//  Created by 郭明皓 on 15/11/14.
//  Copyright © 2015年 郭明皓工作室. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain)UIImageView *imageView;

@end

@implementation ViewController
{
    CIContext *context;
    CIFilter *filter;
    CIImage *beginImage;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.imageView = [[UIImageView alloc]init];
    _imageView.frame = CGRectMake(100, 100, 150, 150);
    [self.view addSubview:_imageView];
    
    
    NSString *filePath =
    [[NSBundle mainBundle] pathForResource:@"beautiful" ofType:@"png"];
    NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath];
    
    beginImage =
    [CIImage imageWithContentsOfURL:fileNameAndPath];
    
    // 1
    context = [CIContext contextWithOptions:nil];
    
//    filter = [CIFilter filterWithName:@"CIPhotoEffectInstant"
//                                  keysAndValues: kCIInputImageKey, beginImage, nil];
    
//    filter = [CIFilter filterWithName:@"CIPhotoEffectTransfer"
//                        keysAndValues: kCIInputImageKey, beginImage, nil];

    filter = [CIFilter filterWithName:@"CISRGBToneCurveToLinear"
                        keysAndValues: kCIInputImageKey, beginImage, nil];
    
//    filter = [CIFilter filterWithName:@"CISepiaTone"
//                                  keysAndValues: kCIInputImageKey, beginImage,
//                        @"inputIntensity", @0.8, nil];
    CIImage *outputImage = [filter outputImage];
    
    // 2
    CGImageRef cgimg =
    [context createCGImage:outputImage fromRect:[outputImage extent]];
    
    // 3
    UIImage *newImage = [UIImage imageWithCGImage:cgimg];
    self.imageView.image = newImage;
    
    // 4  释放
    CGImageRelease(cgimg);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
